package fair.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import fair.dao.OrderSettingsDao;
import fair.pojo.OrderSetting;
import fair.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author fair_wu
 * @version v1.0
 * @date 2021-01-27 上午 10:17
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingsDao dao;

    @Override
    public void add(List<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {
            long count = dao.findCountByOrderDate(orderSetting.getOrderDate());
            if (count > 0) {
                dao.editNumbersByOrderDate(orderSetting);
            }else {
                dao.add(orderSetting);
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        Map<String, String> map = new HashMap();
        map.put("dateBegin", date + "-1");
        map.put("dateEnd", date + "-31");
        List<Map> data = new ArrayList<>();

        List<OrderSetting> list = dao.getOrderSettingByMonth(map);
        if (list != null && list.size() > 0) {
              for (OrderSetting orderSetting : list) {
                Map<String, Object> orderSettingMap = new HashMap();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(orderSetting.getOrderDate());
                orderSettingMap.put("date", calendar.get(Calendar.DATE));
                orderSettingMap.put("number", orderSetting.getNumber());
                orderSettingMap.put("reservations", orderSetting.getReservations());
                data.add(orderSettingMap);
              }
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = dao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0)
            dao.editNumbersByOrderDate(orderSetting);
        else
            dao.add(orderSetting);
    }

    @Override
    public void deletePastOrder(String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        dao.deletePastOrder(map);
    }
}
